﻿using System;
using System.Collections.Generic;
using DSLPedEditorTest.FakeObjects;
using Dsl.Ped.Editor.Services;
using Dsl.Ped.Framework.AddIns;

using NUnit.Framework;

namespace DSLPedEditorTest.Services
{
    [TestFixture]
    public class AddInRegistrationServiceTest
    {
        [SetUp]
        public void SetUp()
        {
            
        }

        [Test]
        public void GetRegisteredTypeTest()
        {
            var ns = "http://abc";

            List<Lazy<IDslPedAddIn, IDslPedAddInMetadata>> addIns = new List<Lazy<IDslPedAddIn, IDslPedAddInMetadata>>();
            addIns.Add(new Lazy<IDslPedAddIn, IDslPedAddInMetadata>(new AddInMetadataFake() { Extension = "abc", Namespace = ns, ProjectiveType = typeof(string) }));

            var addInRegistrationService = new AddInRegistrationService(addIns);
            Type registeredType = addInRegistrationService.GetRegisteredType(ns);

            Assert.AreEqual(typeof(string), registeredType);
        }

        [Test]
        public void GetRegisteredTypeNoneRegisteredTest()
        {
            var ns = "http://abc";

            List<Lazy<IDslPedAddIn, IDslPedAddInMetadata>> addIns = new List<Lazy<IDslPedAddIn, IDslPedAddInMetadata>>();

            var addInRegistrationService = new AddInRegistrationService(addIns);
            Type registeredType = addInRegistrationService.GetRegisteredType(ns);

            Assert.IsNull(registeredType);
        }

        [Test]
        public void GetRegisteredTypeMoreAddInsTest1()
        {
            var nsAbc = "http://abc";
            var nsDef = "http://def";

            List<Lazy<IDslPedAddIn, IDslPedAddInMetadata>> addIns = new List<Lazy<IDslPedAddIn, IDslPedAddInMetadata>>();
            addIns.Add(new Lazy<IDslPedAddIn, IDslPedAddInMetadata>(new AddInMetadataFake() { Extension = "abc", Namespace = nsAbc, ProjectiveType = typeof(string) }));
            addIns.Add(new Lazy<IDslPedAddIn, IDslPedAddInMetadata>(new AddInMetadataFake() { Extension = "def", Namespace = nsDef, ProjectiveType = typeof(int) }));


            var addInRegistrationService = new AddInRegistrationService(addIns);
            Type registeredTypeAbc = addInRegistrationService.GetRegisteredType(nsAbc);
            Assert.AreEqual(typeof(string), registeredTypeAbc);
        }

        [Test]
        public void GetRegisteredTypeMoreAddInsTest2()
        {
            var nsAbc = "http://abc";
            var nsDef = "http://def";

            List<Lazy<IDslPedAddIn, IDslPedAddInMetadata>> addIns = new List<Lazy<IDslPedAddIn, IDslPedAddInMetadata>>();
            addIns.Add(new Lazy<IDslPedAddIn, IDslPedAddInMetadata>(new AddInMetadataFake() { Extension = "abc", Namespace = nsAbc, ProjectiveType = typeof(string) }));
            addIns.Add(new Lazy<IDslPedAddIn, IDslPedAddInMetadata>(new AddInMetadataFake() { Extension = "def", Namespace = nsDef, ProjectiveType = typeof(int) }));

            var addInRegistrationService = new AddInRegistrationService(addIns);
            Type registeredTypeDef = addInRegistrationService.GetRegisteredType(nsDef);
            Assert.AreEqual(typeof(int), registeredTypeDef);
        }

        [Test]
        public void GetRegisteredTypesMoreAddIns()
        {
            var nsAbc = "http://abc";
            var nsDef = "http://def";

            List<Lazy<IDslPedAddIn, IDslPedAddInMetadata>> addIns = new List<Lazy<IDslPedAddIn, IDslPedAddInMetadata>>();
            addIns.Add(new Lazy<IDslPedAddIn, IDslPedAddInMetadata>(new AddInMetadataFake() { Extension = "abc", Namespace = nsAbc, ProjectiveType = typeof(string) }));
            addIns.Add(new Lazy<IDslPedAddIn, IDslPedAddInMetadata>(new AddInMetadataFake() { Extension = "def", Namespace = nsDef, ProjectiveType = typeof(int) }));

            var addInRegistrationService = new AddInRegistrationService(addIns);
            Dictionary<string, Type> registeredTypes = addInRegistrationService.GetRegisteredTypes();

            Assert.AreEqual(2, registeredTypes.Count);
            Assert.AreEqual(typeof(string), registeredTypes[nsAbc]);
            Assert.AreEqual(typeof(int), registeredTypes[nsDef]);
        }

        [Test]
        public void GetNamespaceByTypeTest()
        {
            var nsAbc = "http://abc";
            var nsDef = "http://def";

            List<Lazy<IDslPedAddIn, IDslPedAddInMetadata>> addIns = new List<Lazy<IDslPedAddIn, IDslPedAddInMetadata>>();
            addIns.Add(new Lazy<IDslPedAddIn, IDslPedAddInMetadata>(new AddInMetadataFake() { Extension = "abc", Namespace = nsAbc, ProjectiveType = typeof(string) }));
            addIns.Add(new Lazy<IDslPedAddIn, IDslPedAddInMetadata>(new AddInMetadataFake() { Extension = "def", Namespace = nsDef, ProjectiveType = typeof(int) }));

            var addInRegistrationService = new AddInRegistrationService(addIns);
            var ns = addInRegistrationService.GetNamespaceByType(typeof(string));

            Assert.AreEqual(nsAbc, ns);
        }

        [Test]
        public void GetNamespaceByTypeNonExistingTest()
        {
            var nsAbc = "http://abc";
            var nsDef = "http://def";

            List<Lazy<IDslPedAddIn, IDslPedAddInMetadata>> addIns = new List<Lazy<IDslPedAddIn, IDslPedAddInMetadata>>();
            addIns.Add(new Lazy<IDslPedAddIn, IDslPedAddInMetadata>(new AddInMetadataFake() { Extension = "abc", Namespace = nsAbc, ProjectiveType = typeof(string) }));
            addIns.Add(new Lazy<IDslPedAddIn, IDslPedAddInMetadata>(new AddInMetadataFake() { Extension = "def", Namespace = nsDef, ProjectiveType = typeof(int) }));

            var addInRegistrationService = new AddInRegistrationService(addIns);
            var ns = addInRegistrationService.GetNamespaceByType(typeof(long));

            Assert.AreEqual("", ns);
        }
    }
}
